package com.zndroid.gridview.load;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zndroid.gridview.IImageBean;
import com.zndroid.gridview.load.impl.HttpBigImageUrlLoader;
import com.zndroid.gridview.load.impl.HttpSmallImageUrlLoader;
import com.zndroid.gridview.load.impl.LocalFileLoader;
import com.zndroid.gridview.load.impl.LocalResIdLoader;

import java.lang.ref.WeakReference;

/**
 * 责任链模式 + 单例模式
 * 图片加载管理
 * */
public final class ImageLoader {
    LocalResIdLoader im_boss;

    private ImageLoader() {
        //规则，可以在此调整规则
        //本地资源ID > 本地资源文件 > 网络缩略图 > 网络大图

        LocalResIdLoader localResIdLoader = new LocalResIdLoader();
        LocalFileLoader localFileLoader = new LocalFileLoader();
        HttpSmallImageUrlLoader httpSmallImageUrlLoader = new HttpSmallImageUrlLoader();
        HttpBigImageUrlLoader httpBigImageUrlLoader = new HttpBigImageUrlLoader();

        localResIdLoader.setNextEngine(localFileLoader);
        localFileLoader.setNextEngine(httpSmallImageUrlLoader);
        httpSmallImageUrlLoader.setNextEngine(httpBigImageUrlLoader);

        //入口
        im_boss = localResIdLoader;
    }

    private static final class SELF {
        public static final ImageLoader MY_SELF = new ImageLoader();
    }

    public static ImageLoader getLoader() {
        return SELF.MY_SELF;
    }

    public void load(
            @NonNull WeakReference<Context> context,
            @NonNull ImageView imageView,
            @NonNull IImageDisplayEngine engine,
            @NonNull IImageBean bean,
            @Nullable ILoadCallBack loadCallBack)
    {
        im_boss.load(context.get(), imageView, engine, bean, loadCallBack);
    }

}
